package com.ly.lyyudao.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.ly.lyyudao.domain.pojo.Users;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @auth wzy
 * @date 2024/4/26 17:08
 */
public class LoginUser implements UserDetails {

    private Users users;

    @JSONField(serialize = false)
    private Set<GrantedAuthority> authorities;

    private List<String> perms;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if (authorities != null){
            return authorities;
        }

        if (perms==null){
            return null;
        }

        authorities = new HashSet<>();

        for (String perm : perms) {
            if (StringUtils.hasText(perm)) {
                authorities.add(new SimpleGrantedAuthority(perm));
            }
        }

        return authorities;
    }

    public LoginUser() {
    }

    public LoginUser(Users users, List<String> perms) {
        this.users = users;
        this.perms=perms;
    }

    public List<String> getPerms() {
        return perms;
    }

    public void setPerms(List<String> perms) {
        this.perms = perms;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }




    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
